package cn.yeyiu.netonlinevideo.parser.blog;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import cn.yeyiu.netonlinevideo.util.BlogUtil;
import cn.yeyiu.util.bean.BlogInfo;
import cn.yeyiu.netonlinevideo.bean.SourceData;
import cn.yeyiu.netonlinevideo.parser.Parser;
import cn.yeyiu.netonlinevideo.viewmodel.DataListViewModel;
import cn.yeyiu.util.NetUtil;
import cn.yeyiu.util.bean.MediaData;

public class LlssParser extends Parser {

    public LlssParser(SourceData source) {
        super(source);
    }

    @Override
    public Object getBaseData(String type, int pageIndex) {
        String path = getArgv(type, "path");
        String html = NetUtil.getByUrl(source.url + path
                + String.format(Locale.CHINA, "page/%d/", pageIndex + 1));

        return Jsoup.parse(html);
    }

    @Override
    public int getMaxCount(Object data) {
        Document document = (Document) data;
        String pageCountStr = document.selectXpath("//div[@class='nav-links']/a[last()-1]").first().text();
        pageCountStr = pageCountStr.replace("页 ", "");
        return Integer.parseInt(pageCountStr);
    }

    @Override
    public List<MediaData> getDataList(Object data, String type) {
        Document document = (Document) data;
        Elements uls = document.getElementsByTag("article");
        if (uls.size() == 0) {
            return null;
        }

        List<MediaData> dataList = new ArrayList<>();
        MediaData mediaData;
        BlogInfo blogInfo;
        for (Element li : uls) {
            blogInfo = new BlogInfo();
            mediaData = new MediaData();

            mediaData.code = source.code;
            Elements a = li.selectXpath("./article/header/h2");
            if (a.size() == 0) {
                continue;
            }
            mediaData.title = li.selectXpath("./article/header/h2/a").get(0).text();
            mediaData.description = li.selectXpath("./article/div/p").get(0).text();
            mediaData.logo = li.selectXpath("./article/div/p/img").get(0).attr("src");
            mediaData.detail = li.selectXpath("./article/div/p/a").get(0).attr("href");
            // time

            Elements id = li.selectXpath("./article/footer/span");
            String time = id.get(0).selectXpath("./span/a/time").first().text();
            String cat = id.get(2).selectXpath("./span/a").text();
            String tag = id.get(3).selectXpath("./span/a").text();
            mediaData.id = ">> " + time
                    + "\n>> " + cat
                    + "\n>> " + tag;

            blogInfo.title = mediaData.title;
            blogInfo.logo = mediaData.logo;
            blogInfo.url = mediaData.detail;

            mediaData.data = blogInfo;
            dataList.add(mediaData);
        }
        return dataList;
    }

    @Override
    public Object buildData(MediaData mediaData) {
        BlogInfo blogInfo = (BlogInfo) mediaData.data;

        String html = NetUtil.getByUrl(mediaData.detail);
        Document document = Jsoup.parse(html);

        // build blog view
        Elements elements = document.selectXpath("//article/div[@class='entry-content']/*");
        List<Node> nodes = new ArrayList<>();
        for (int i = 1; i < elements.size() - 2; ++i) {
            nodes.add(elements.get(i));
        }
        blogInfo.viewList = new ArrayList<>();
        BlogUtil.buildView(blogInfo.viewList, nodes);

        // build comment
        Elements comments = document.selectXpath("//div[@class='wpd-thread-list']/div[contains(@id,'wpd')]");
        blogInfo.commentList = new ArrayList<>();
        BlogUtil.buildComment(blogInfo.commentList, comments);

        return blogInfo;
    }
}
